// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CMSHTMLDOMPurger.
//   $Id$
#include "stdafx.h"
#include "mshtmldompurger.h"
#include "ExecuteDispatch.h"
#include <common/FileSystem/msw/FileSystemUtils.h>
#include <Common/ResourceHandling/msw/loadHTMLResource.h>

CMSHTMLDOMPurger * CMSHTMLDOMPurger::sm_singleton = 0;
HANDLE CMSHTMLDOMPurger::sm_hSingletonMutex = 0;

static const CComBSTR cg_sPurge = L"FP_PURGE_ATTACHED_EVENTS";
static const CComBSTR cg_sJavascript = L"JScript";
static const CComBSTR cg_sAlert =  L"function alert(s) { window.alert(s); }";

CMSHTMLDOMPurger::CMSHTMLDOMPurger(void)
:m_didPurge(0)
{
}

CMSHTMLDOMPurger::~CMSHTMLDOMPurger(void)
{
}

HRESULT CMSHTMLDOMPurger::Purge(IDispatch *pElement)
{
	HRESULT hr = E_FAIL;

	if(!m_pPurgeScriptDispatch)
	{
		hr = loadPurgeScript();
	}

	if(m_pPurgeScriptDispatch)
	{
		try
		{
			CComVariant vElement = pElement;
			DISPPARAMS params = { &vElement, 0, 1, 0 };
			hr = InvokeDispatch(m_pPurgeScriptDispatch, cg_sPurge, &params, 0, m_didPurge, DISPATCH_METHOD, LOCALE_USER_DEFAULT, 0, 0);
		}
		catch(...)
		{
		}
	}

	return hr;
}

CMSHTMLDOMPurger *CMSHTMLDOMPurger::GetPurger(void)
{
	if(!sm_hSingletonMutex)
		sm_hSingletonMutex = CreateMutex(0, FALSE, _T("Local\\MSHTMLDOMPurgerSingletonMutex"));

	WaitForSingleObject(sm_hSingletonMutex, INFINITE);

	try
	{
		if(!sm_singleton)
		{
			sm_singleton = new CMSHTMLDOMPurger();
		}
	}
	catch(...)
	{
	}

	ReleaseMutex(sm_hSingletonMutex);

	return sm_singleton;
};

void CMSHTMLDOMPurger::Clear(void)
{
	if(!sm_hSingletonMutex)
		sm_hSingletonMutex = CreateMutex(0, FALSE, _T("Local\\MSHTMLDOMPurgerSingletonMutex"));

	WaitForSingleObject(sm_hSingletonMutex, INFINITE);

	try
	{
		if(sm_singleton)
		{
			delete sm_singleton;
			sm_singleton = 0;
		}
	}
	catch(...)
	{
	}

	ReleaseMutex(sm_hSingletonMutex);
};

HRESULT CMSHTMLDOMPurger::loadPurgeScript()
{
	HRESULT hr = m_pPurgeScript.CoCreateInstance(CLSID_ScriptEngine);
	if(SUCCEEDED(hr))
	{
		m_pPurgeScript->loadScriptText(cg_sJavascript, cg_sAlert);

		CComBSTR sScript;
		xport::loadHTMLResource(::_Module.m_hInst, "purge.js", sScript);
		CComQIPtr<IScriptEngine2> pScript2 = m_pPurgeScript;
		hr = pScript2->addScriptText(sScript);
		if(SUCCEEDED(hr))
			hr = m_pPurgeScript->GetScriptObject(&m_pPurgeScriptDispatch);
	}

	return hr;
}

